package com.wanmait.community.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.community.entity.AdminRepair;
import com.wanmait.community.entity.BeforeRepairPic;
import com.wanmait.community.entity.UserRepair;
import com.wanmait.community.mapper.AdminRepairMapper;
import com.wanmait.community.mapper.BeforeRepairPicMapper;
import com.wanmait.community.mapper.UserRepairMapper;
import com.wanmait.community.service.AdminRepairService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.time.ZoneId;
import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class AdminRepairServiceImpl extends ServiceImpl<AdminRepairMapper, AdminRepair> implements AdminRepairService{
    @Resource
    private AdminRepairMapper adminRepairMapper;
    @Resource
    private UserRepairMapper userRepairMapper;
    @Resource
    private BeforeRepairPicMapper beforeRepairPicMapper;
    @Override
    public PageInfo<AdminRepair> findAll(AdminRepair adminRepair, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Date endCreatTime = null;
        Date endUpdateTime = null;
        // 处理 creatTime
        if (adminRepair.getCreatTime() != null) {
            endCreatTime = Date.from(
                    adminRepair.getCreatTime().toInstant()
                            .atZone(ZoneId.systemDefault())
                            .toLocalDateTime()
                            .withHour(23).withMinute(59).withSecond(59)
                            .atZone(ZoneId.systemDefault())
                            .toInstant()
            );
        }
        // 处理 updateTime
        if (adminRepair.getUpdateTime() != null) {
            endUpdateTime = Date.from(
                    adminRepair.getUpdateTime().toInstant()
                            .atZone(ZoneId.systemDefault())
                            .toLocalDateTime()
                            .withHour(23).withMinute(59).withSecond(59)
                            .atZone(ZoneId.systemDefault())
                            .toInstant()
            );
        }
        List<AdminRepair> adminRepairs = adminRepairMapper.selectAdminRepairInfo(adminRepair,endCreatTime,endUpdateTime);
        return new PageInfo<>(adminRepairs,7);
    }
//只添加creatBy,其他自动生成（不添加userRepair），(添加报修信息，所有关联表都加记录）
    @Override
    public void insertAdminRepair(AdminRepair adminRepair,String beforePic) {
        adminRepairMapper.insertAdminRepair(adminRepair);
        //userRepair设置值
        UserRepair userRepair = new UserRepair();
        userRepair.setUserId(adminRepair.getCreatBy());
        userRepair.setContent(adminRepair.getUserRepair().getContent());
        userRepair.setAdminRepairId(adminRepair.getId());
        //beforeRepairPic设置值
        BeforeRepairPic beforeRepairPic = new BeforeRepairPic();
        beforeRepairPic.setCreatBy(adminRepair.getCreatBy());
        beforeRepairPic.setAdminRepairId(adminRepair.getId());
        beforeRepairPic.setBeforePic(beforePic);
        //添加不需要往afterRepairPic里加！
        //主键回填到adminRepairId
        userRepairMapper.insertUserRepair(userRepair);
        beforeRepairPicMapper.insertBeforeRepairPic(beforeRepairPic);
    }
    @Override
    public AdminRepair findAdminRepairById(Integer id) {
        return adminRepairMapper.findAdminRepairById(id);
    }
    @Override
    public void updateAdminRepairById(AdminRepair adminRepair) {//业主修改报修信息需要修改adminRepair的更新人更新时间
        adminRepairMapper.updateAdminRepairById(adminRepair);
    }
    @Override
    public void deleteAdminRepairById(Integer id) {//业主逻辑删除自己交过的报修
        adminRepairMapper.deleteAdminRepairById(id);
    }

    @Override
    public void updateHandleRepair(AdminRepair adminRepair) {//物业员工处理维修
        Date now = new Date();
        adminRepair.setRepairTime(now);
        adminRepair.setRepairStatus(true);
        adminRepairMapper.updateHandleRepair(adminRepair);
    }
}
